package com.dog.framework.email.service.impl;

import com.alibaba.fastjson2.JSON;
import com.dog.framework.core.enums.ResponseCodeEnum;
import com.dog.framework.email.domain.MailSendEntity;
import com.dog.framework.email.domain.MailSendResult;
import com.dog.framework.email.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;


@Slf4j
//@Service
public class MailServiceImpl implements MailService {

    @Autowired
    private JavaMailSenderImpl mailSender;

    @Override
    public MailSendResult sendMail(MailSendEntity entity) {
        MailSendResult result = new MailSendResult();
        if (ObjectUtils.isEmpty(entity.getTo())) {
            result.setSuccess(false);
            result.setMessage(ResponseCodeEnum.MAIL_RECEIVER_NOT_EMPTY.getMsg());
            return result;
        }
        if (ObjectUtils.isEmpty(entity.getSubject())) {
            result.setSuccess(false);
            result.setMessage(ResponseCodeEnum.MAIL_TOPIC_NOT_EMPTY.getMsg()); //
            return result;
        }
        if (entity.getText()==null) {
            result.setSuccess(false);
            result.setMessage(ResponseCodeEnum.MAIL_CONTENT_NOT_EMPTY.getMsg());
            return result;
        }
        try {

            //true表示支持复杂类型
            MimeMessageHelper messageHelper = new MimeMessageHelper(mailSender.createMimeMessage(), true);
            //邮件发信人
            entity.setFrom(mailSender.getUsername());
            messageHelper.setFrom(mailSender.getUsername());
            //邮件收信人
            messageHelper.setTo(entity.getTo().split(","));
            //邮件主题
            messageHelper.setSubject(entity.getSubject());
            //邮件内容
            messageHelper.setText(entity.getText());
            if (!ObjectUtils.isEmpty(entity.getCc())) {
                //抄送
                messageHelper.setCc(entity.getCc().split(","));
            }
            if (!ObjectUtils.isEmpty(entity.getBcc())) {
                //密送
                messageHelper.setCc(entity.getBcc().split(","));
            }
            if (entity.getMultipartFiles() != null) {
                //添加邮件附件
                for (MultipartFile multipartFile : entity.getMultipartFiles()) {
                    messageHelper.addAttachment(multipartFile.getOriginalFilename(), multipartFile);
                }
            }
            if (entity.getSentDate() == null) {
                //发送时间
                entity.setSentDate(new Date());
                messageHelper.setSentDate(entity.getSentDate());
            }
            log.info("send mail: [{}]", JSON.toJSONString(entity));
            //正式发送邮件
            mailSender.send(messageHelper.getMimeMessage());
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }

}
